package com.masterclient.app.modules.coupon;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.widget.empty.FCPageStateView;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.modules.coupon.adapter.GetCoupanRecycleAdapter;
import com.masterclient.app.modules.coupon.beans.NewCouponBean;
import com.masterclient.app.modules.coupon.contract.CouponContract;
import com.masterclient.app.modules.coupon.contract.CouponPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/4 13:17
 * 功能说明：
 */
@Route(path = AroutePath.GET_COUPON_FRAGMENT)
public class GetCouponFragment extends BaseFragment<CouponPresenter> implements CouponContract.View {

    public static final String UNUSE = "unuse";
    public static final String USED = "used";
    public static final String USELESS = "useless";
    String type;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.empty_view)
    FCPageStateView emptyView;

    List<NewCouponBean.CouponBean> list;
    private boolean hasMore;
    private int page = 1;

    private GetCoupanRecycleAdapter recycleAdapter;

    public static GetCouponFragment getInstance(String type) {
        GetCouponFragment couponFragment = new GetCouponFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("type", type);
        couponFragment.setArguments(bundle);
        return couponFragment;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_get_coupon, null);
    }

    @Override
    protected void initOnlyOnce() {
        Bundle bundle = getArguments();
        this.type = bundle.getString("type");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recycleAdapter = new GetCoupanRecycleAdapter(type);
        recycleAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_btn) {
                mPresenter.pickUpSalesrule(list.get(position).coupon_code,"");
            }
        });
        recyclerView.setAdapter(recycleAdapter);

        recycleAdapter.bindToRecyclerView(recyclerView);
        emptyView.setViewState(FCPageViewState.PAGE_STATE_NO_DATA,R.mipmap.icon_empty_ticket,"一大波好券正赶来的路上");
        emptyView.setVisibility(View.GONE);

        page = 1;
        mPresenter.getReceiveCouponList(String.valueOf(page), PAGE_SIZE);

        recycleAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (hasMore) {
                    mPresenter.getReceiveCouponList(String.valueOf(page + 1), PAGE_SIZE);
                }
            }
        }, recyclerView);

        swipeLayout.setOnRefreshListener(() -> {

            if (list != null) {
                list.clear();
                recycleAdapter.notifyDataSetChanged();
            }
            emptyView.setVisibility(View.GONE);
            recycleAdapter.setEnableLoadMore(true);
            page = 1;
            mPresenter.getReceiveCouponList(String.valueOf(page), PAGE_SIZE);

        });
        super.initOnlyOnce();
    }

    @OnClick(R.id.rl_back)
    protected void onClick(View view) {
        if (view.getId() ==R.id.rl_back){
            getActivity().finish();
        }
    }

    @Override
    public void onCouponListSuccess(NewCouponBean dataBean) {

    }

    @Override
    public void onReceiveCouponListSuccess(NewCouponBean dataBean) {
        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
        hasMore = dataBean.data.hasMore;
        this.page = dataBean.data.page;

        if (list == null) {
            list = new ArrayList<>();
        }

        if (page == 1){
            list.clear();
        }

        list.addAll(dataBean.data.couponList);
        recycleAdapter.setNewData(list);
        if (list.size() <= 0) {
            emptyView.setVisibility(View.VISIBLE);
        }
        if (!hasMore) {
            recycleAdapter.setEnableLoadMore(false);
        }
        recycleAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadFailed(String msg) {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
        emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void pickUpSalesruleSuccess(String coupon_code, String msg) {
        showToast(msg);
        page = 1;
        mPresenter.getReceiveCouponList(String.valueOf(page), PAGE_SIZE);
    }

    @Override
    public void pickUpSalesruleFailed(String msg) {
        showToast(msg);
    }
}
